package org.jeecg.modules.controller.market;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.entity.DbCustomer;
import org.jeecg.modules.entity.vo.CusCountVo;
import org.jeecg.modules.service.IDbCustomerService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: db_customer
 * @Author: jeecg-boot
 * @Date:   2024-06-13
 * @Version: V1.0
 */
@Api(tags="客户管理")
@RestController
@RequestMapping("/dbCustomer")
@Slf4j
public class DbCustomerController extends JeecgController<DbCustomer, IDbCustomerService> {
	@Autowired
	private IDbCustomerService dbCustomerService;

	 /**
	  * 客户统计
	  * @param type
	  * @return
	  */
	@ApiOperation(value="客户统计", notes="客户统计")
	@GetMapping("/cusCount")
	public Result<CusCountVo> cusCount(@RequestParam Integer type){
		CusCountVo cusCountVo =dbCustomerService.cusCount(type);
		return Result.OK(cusCountVo);
	}

	/**
	 * 查询客户名称列表
	 * @return
	 */
	@ApiOperation(value="查询客户名称列表", notes="查询客户名称列表")
	@GetMapping("/getNameList")
	public Result<List<String>> getCusNameList() {
		List<String> list = dbCustomerService.getCusNameList();
		return Result.OK(list);
	}
	/**
	 * 分页列表查询
	 *
	 * @param dbCustomer
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "db_customer-分页列表查询")
	@ApiOperation(value="分页列表查询客户", notes="分页列表查询客户")
	@GetMapping(value = "/list")
	public Result<IPage<DbCustomer>> queryPageList(DbCustomer dbCustomer,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
//		QueryWrapper<DbCustomer> queryWrapper = new QueryWrapper<>();
		QueryWrapper<DbCustomer> queryWrapper = QueryGenerator.initQueryWrapper(dbCustomer, req.getParameterMap());
		queryWrapper.orderByDesc("create_time");
		Page<DbCustomer> page = new Page<DbCustomer>(pageNo, pageSize);
		IPage<DbCustomer> pageList = dbCustomerService.page(page, queryWrapper);
		System.out.println(queryWrapper.getSqlSegment());
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param dbCustomer
	 * @return
	 */
	@AutoLog(value = "db_customer-添加")
	@ApiOperation(value="添加客户", notes="添加客户")
	//@RequiresPermissions("org.jeecg.modules.demo:db_customer:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody DbCustomer dbCustomer) {
		dbCustomerService.save(dbCustomer);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param dbCustomer
	 * @return
	 */
	@AutoLog(value = "db_customer-编辑")
	@ApiOperation(value="编辑客户", notes="编辑客户")
	//@RequiresPermissions("org.jeecg.modules.demo:db_customer:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody DbCustomer dbCustomer) {
		dbCustomerService.updateById(dbCustomer);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "db_customer-通过id删除")
	@ApiOperation(value="通过id删除客户", notes="通过id删除客户")
	//@RequiresPermissions("org.jeecg.modules.demo:db_customer:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		dbCustomerService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "db_customer-批量删除")
	@ApiOperation(value="批量删除客户", notes="批量删除客户")
	//@RequiresPermissions("org.jeecg.modules.demo:db_customer:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.dbCustomerService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "db_customer-通过id查询")
	@ApiOperation(value="通过id查询客户", notes="通过id查询客户")
	@GetMapping(value = "/queryById")
	public Result<DbCustomer> queryById(@RequestParam(name="id",required=true) String id) {
		DbCustomer dbCustomer = dbCustomerService.getById(id);
		if(dbCustomer==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(dbCustomer);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param dbCustomer
    */
    //@RequiresPermissions("org.jeecg.modules.demo:db_customer:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, DbCustomer dbCustomer) {
        return super.exportXls(request, dbCustomer, DbCustomer.class, "db_customer");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    //@RequiresPermissions("db_customer:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, DbCustomer.class);
    }

}
